package net.lastcoder.ifserver.nio;

import net.lastcoder.ifserver.Globals;
import net.lastcoder.ifserver.Server;
import net.lastcoder.ifserver.database.DbConnectionManager;
import net.lastcoder.ifserver.module.BaseModule;
import net.lastcoder.ifserver.module.ModuleNames;
import net.lastcoder.ifserver.util.TextUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.StringTokenizer;

/**
 * auth: WenYF
 * date: 2017/3/31
 */
public class ConnectPrivacy extends BaseModule {
    private static final Logger logger = LoggerFactory.getLogger(ConnectPrivacy.class);
    private static class SingletonHolder {
        static ConnectPrivacy instance = new ConnectPrivacy();
    }

    private String columnNme = null;
    private String loadSql = null;
    private String insertIp = null;
    private String deleteSql = null;

    private ConnectPrivacy() {
        super(ModuleNames.ConnectPrivacy);
        bans = new ArrayList<>();
        loadSql = Globals.getXMLProperty("privacy.bans.sql.load");
        insertIp = Globals.getXMLProperty("privacy.bans.sql.insert");
        deleteSql = Globals.getXMLProperty("privacy.bans.sql.delete");
        columnNme = Globals.getXMLProperty("privacy.bans.sql.name");
    }

    public static ConnectPrivacy getInstance() {
        return SingletonHolder.instance;
    }

    private Collection<String> bans;
    @Override
    public void init() {
        // 加载被禁ip
//        Connection connection = null;
//        PreparedStatement pstmt = null;
//        ResultSet rs = null;
//        try {
//            connection = DbConnectionManager.getConnection();
//            if (TextUtils.isEmpty(loadSql) || TextUtils.isEmpty(columnNme)) {
//                return;
//            }
//            pstmt = connection.prepareStatement(loadSql);
//            rs = pstmt.executeQuery();
//            while (rs.next()) {
//                bans.add(rs.getString(columnNme));
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            logger.error("may be have not database config.", e);
//        } finally {
//            DbConnectionManager.closeConnection(rs, pstmt, connection);
//        }

        // 加载默认的
        String str = Globals.getXMLProperty("privacy.bans.ip");
        if (!TextUtils.isEmpty(str)) {
            StringTokenizer tokenizer = new StringTokenizer(str, ";");
            while (tokenizer.hasMoreTokens()) {
                str = tokenizer.nextToken();
                if (TextUtils.isEmpty(str)) {
                    bans.add(str);
                }
            }
        }
    }

    @Override
    public void reload() {
        destroy();
        init();
    }

    @Override
    public void destroy() {
        bans.clear();
    }

    public boolean isBan(String ip) {
        return bans.contains(ip);
    }

    public void banIP(String ip) {
        bans.add(ip);
        insertIp(ip);
    }

    public void unbanIP(String ip) {
        bans.remove(ip);
        deleteIp(ip);
    }

    public Collection<String> getBanIPs() {
        return bans;
    }

    private void deleteIp(String ip) {
//        Connection connection = null;
//        PreparedStatement pstmt = null;
//        try {
//            connection = DbConnectionManager.getConnection();
//            if (TextUtils.isEmpty(deleteSql) || TextUtils.isEmpty(columnNme)) {
//                return;
//            }
//            pstmt = connection.prepareStatement(deleteSql);
//            pstmt.setString(1, ip);
//            pstmt.executeUpdate();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            DbConnectionManager.closeConnection(pstmt, connection);
//        }
    }

    private void insertIp(String ip) {
//        Connection connection = null;
//        PreparedStatement pstmt = null;
//        try {
//            connection = DbConnectionManager.getConnection();
//            if (TextUtils.isEmpty(insertIp) || TextUtils.isEmpty(columnNme)) {
//                return;
//            }
//            pstmt = connection.prepareStatement(insertIp);
//            pstmt.setString(1, ip);
//            pstmt.executeUpdate();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            DbConnectionManager.closeConnection(pstmt, connection);
//        }
    }
}
